package online.xuping.center.account.impl;

import online.xuping.center.account.AccountErrorCodeEnum;
import online.xuping.center.account.IAccountStateService;
import online.xuping.center.account.domain.AccountState;
import online.xuping.center.account.domain.TradeType;
import online.xuping.common.SettingUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
@Service
public class AccountStateServiceImpl implements IAccountStateService {
    @Override
    public AccountState initAccountState(String accountTypeName) {
        if(accountTypeName==null){
            throw    AccountErrorCodeEnum.ACCOUNT_TYPE_SETTING_ERROR.exception();
        }
        List<String> list = SettingUtil.getList("config/account.setting", "accountTypeTradeType");
        String reString=null;
        for(String it :list){
            if (it.startsWith(accountTypeName)) {
                reString = it;
            }
        }
        if(reString==null){
            throw    AccountErrorCodeEnum.ACCOUNT_TYPE_SETTING_ERROR.exception();
        }
        //like this 普卡:充值|支付|转账|提现
        String[] data = reString.split(":");
        if (data == null || data.length != 2) {
            throw    AccountErrorCodeEnum.ACCOUNT_TYPE_SETTING_ERROR.exception();
        }
        String[] type = data[1].split("\\|");
        if (type == null || type.length ==0 ) {
            throw    AccountErrorCodeEnum.ACCOUNT_TYPE_SETTING_ERROR.exception();
        }
        AccountState accountState = new AccountState();
        List<TradeType> canDo = new ArrayList<>();
        for (int i = 0; i < type.length; i++) {
            canDo.add(TradeType.builder().tradeTypeName(type[i]).build());
        }
        accountState.setCanDo(canDo);
        return accountState;
    }
}
